local Luajieyin = fk.CreateSkill {
  name = "Luajieyin",
  tags = { Skill.Limited },
}

Fk:loadTranslationTable {
  ["Luajieyin"] = "接引",
  ["@@Luajieyin"] = "接引操控",
  ["#Luajieyin"] = "接引：你可操控一名角色至你死亡。",
  [":Luajieyin"] = "限定技，游戏开始前你可选择一名其他角色，操作目标直至你死亡。",

  ["$Luajieyin1"] = "我在西方以慧眼相观，见东南二处有百余道红光冲天，知是有缘。",
}

Luajieyin:addEffect(fk.GamePrepared, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(Luajieyin.name) and player:usedSkillTimes(Luajieyin.name, Player.HistoryGame) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local selected = room:askToChoosePlayers(player, {
      targets = room:getOtherPlayers(player),
      min_num = 1,
      max_num = 1,
      prompt = "#Luajieyin",
      skill_name = Luajieyin.name
    })
    if #selected ~= 0 then
      event:setCostData(self, { tos = selected })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local dat = event:getCostData(self).tos[1]
    local room = player.room
    room:setPlayerMark(dat, "@@Luajieyin", 1)
    player:control(dat)
  end,
})

Luajieyin:addEffect(fk.Death, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    local jy = false
    local room = player.room
    for _, p in ipairs(room:getAlivePlayers()) do
      if p:getMark("@@Luajieyin") > 0 then
        jy = true
      end
    end
    return player:hasSkill(Luajieyin.name, true, true) and target == player and jy
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room:getAlivePlayers()) do
      if p:getMark("@@Luajieyin") > 0 then
        room:setPlayerMark(p, "@@Luajieyin", 0)
        p:control(p)
      end
    end
  end,
})

return Luajieyin
